function sqlitehelper(dbpath) {
    var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database(dbpath);
    db.run("CREATE TABLE IF NOT EXISTS `apks` ( `pkgname` TEXT, `vercode` TEXT, `vername` TEXT, \
        `ip` TEXT, `remark` TEXT, `patchedpath` TEXT, `originpath` TEXT, `mmver` TEXT, `status` TEXT, PRIMARY KEY(`pkgname`,`vercode`,`vername`,`ip`,`remark`) )");


    this.insert = function (pkgname, vercode, vername, ip, remark, patchedpath, originpath, mmver, status) {
        db.run("insert or replace into  apks values(?,?,?,?,?,?,?,?,?)", pkgname, vercode, vername, ip, remark, patchedpath, originpath, mmver, status);
    };

    this.delete = function (pkgname, vercode, vername, ip, remark) {
        try {
            db.run("delete from apks where pkgname=? and vercode=? and vername=? and ip=? and remark=?", pkgname, vercode, vername, ip, remark);    
        } catch (error) {
            
        }
    };

    this.getPath = function(pkgname, vercode, vername, ip, remark, cb) { 
        console.log("getpath=== "+ pkgname + " " + vercode + " " + vername + " " + ip + " " + remark);
        db.all("select patchedpath,status from apks where pkgname=? and vercode=? and vername=? and ip=? and remark=?", pkgname, vercode, vername, ip, remark, function (err, row) {
            console.log(err, row);
            if(cb){
                if(row && row.length>0){
                    cb(row[0].patchedpath, row[0].status);
                }else{
                    cb(null);
                }
            }

        });
    }; 
};

module.exports = sqlitehelper;
